package com.hksj.call.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hksj.call.entity.CallOrder;
import com.hksj.call.mapper.CallOrderMapper;
import com.hksj.call.query.CallOrderQuery;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 *  服务类
 * @author chenxingqing
 */
@Service
public class CallOrderService extends ServiceImpl<CallOrderMapper, CallOrder> {

    /**
     * 发布/完成任务
     * @param callOrder
     */
    public void saveCallOrder(CallOrder callOrder) {
        //发布任务
        if (callOrder.getStatus() == 0){
            CallOrder toComplete = lambdaQuery()
                    .eq(CallOrder::getCode, callOrder.getCode())
                    .eq(CallOrder::getStatus, 0)
                    .last("limit 1")
                    .one();
            if (!ObjectUtils.isEmpty(toComplete)) {
                toComplete.setPlayStatus(0);
                toComplete.setRoomName(callOrder.getRoomName());
                toComplete.setRemark(callOrder.getRemark());
                updateById(toComplete);
            }else{
                callOrder.setPlayStatus(0);
                save(callOrder);
            }
        }else if (callOrder.getStatus() == 1){
            //完成任务
            lambdaUpdate()
                    .eq(CallOrder::getCode, callOrder.getCode())
                    .eq(CallOrder::getStatus, 0)
                    .set(CallOrder::getStatus, callOrder.getStatus())
                    .set(CallOrder::getPlayStatus,1)
                    .update();
        }
    }

    public Page<CallOrder> pageCallOrder(CallOrderQuery query) {
        Page<CallOrder> page = new Page<>(query.getPageNum(), query.getPageSize());
        List<CallOrder> list =  baseMapper.pageCallOrder(query,query.getDisAblePage() ? null : page);
        page.setRecords(list);
        return page;
    }
}
